package com.quanyan.common.utils;


public class PageUtils {
    // 分页时每页显示多少个
    public static int PAGE_SIZE = 1;

    /**
     * 获得指定页的开始记录数
     *
     * @param page
     * @return
     */
    public static int getBeginIndexByPage(int page) {
        return PAGE_SIZE * (page - 1);
    }

    /**
     * 计算总共有多少页
     *
     * @param totalNum
     * @return
     */
    public static int getTotalPageNum(int totalNum) {
        int temp = totalNum % PAGE_SIZE;
        if (temp == 0) {
            return totalNum / PAGE_SIZE;
        }
        return totalNum / PAGE_SIZE + 1;
    }

    public static int getBeginIndexByPage(int totalNum, int currentPage, int jumpPage) {
        int page = jumpPage;
        //判断是否是刷新
        if (currentPage == jumpPage) {
            page = currentPage;
        }
        int totalPageNum = getTotalPageNum(totalNum);
        if (jumpPage > totalPageNum) {
            page = currentPage;
        }
        return getBeginIndexByPage(page);
    }

    public static int getBeginPage(int totalNum, int currentPage, int jumpPage) {
        int page = jumpPage;
        //判断是否是刷新
        if (currentPage == jumpPage) {
            page = currentPage;
        }
        int totalPageNum = getTotalPageNum(totalNum);
        if (jumpPage > totalPageNum) {
            page = currentPage;
        }
        return page;
    }
}
